package com.yanqu.road.server.cmd.disperseGold;
import com.yanqu.road.entity.disperseGold.DisperseGoldGroupUser;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.disperseGold.DisperseGoldProto;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross.disperseGold.CrossDisperseGoldProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.*;

@Cmd(code = Protocol.C_DISPERSE_GOLD_SEND_INFO, desc = "跨服送财")
public class CrossDisperseGoldCmd extends CrossCommand {

    private CrossDisperseGoldProto.CrossDisperseGoldInfoMsg infoMsg;
    private PlayerProto.PlayerBaseTempMsg userBaseInfoMsg;
    private Set<Long> serverIds = new HashSet<>();

    private List<CrossDisperseGoldProto.CrossDisperseGoldGroupUserTemp> groupUserTempList;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossDisperseGoldProto.CrossDisperseGoldMsg reqMsg = CrossDisperseGoldProto.CrossDisperseGoldMsg.parseFrom(packet.getBytes());
        infoMsg = reqMsg.getInfo();
        userBaseInfoMsg = reqMsg.getPlayerInfo();
        serverIds.addAll(reqMsg.getServerIdsList());
        groupUserTempList = reqMsg.getGroupUserListList();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossDisperseGoldProto.CrossDisperseGoldMsg.Builder msg = CrossDisperseGoldProto.CrossDisperseGoldMsg.newBuilder();
        msg.setInfo(infoMsg);
        msg.setPlayerInfo(userBaseInfoMsg);
        msg.addAllServerIds(serverIds);
        msg.addAllGroupUserList(groupUserTempList);
        // 送财的区服id
        long sendServerId = this.serverId;
        // 先发送到区服
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_DISPERSEGOLD_INFO, msg);
        for (Long serverId : serverIds) {
            // 判断区服的开服时间
            if (sendServerId != serverId && ServerListMgr.getServerOpenDay(serverId) <= ChannelConfig.SERVER_DISPERSE_GOLD_OPEN_DAY) {
                continue;
            }
            MessageHelper.sendPacket(serverId, 0, pbMsg);
        }
        // 再发送名单范围
        // 聚合一下,这边拿区服,具体名单到区服去拿就好了
        Set<Long> groupServerSet = new HashSet<>();
        for (CrossDisperseGoldProto.CrossDisperseGoldGroupUserTemp groupUserTemp : groupUserTempList) {
            if (serverIds.contains(groupUserTemp.getServerId())) {
                continue;
            }
            groupServerSet.add(groupUserTemp.getServerId());
        }
        for (Long serverId : groupServerSet) {
            // 判断区服的开服时间
            if (sendServerId != serverId && ServerListMgr.getServerOpenDay(serverId) <= ChannelConfig.SERVER_DISPERSE_GOLD_OPEN_DAY) {
                continue;
            }
            MessageHelper.sendPacket(serverId, 0, pbMsg);
        }
    }

    @Override
    public void sendData() {

    }
}
